package com.example.demo.Service;

import com.example.demo.Repository.UserRepo;
import com.example.demo.Entity.User;

import org.apache.logging.log4j.message.Message;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserServiceImpl implements UserService{

    UserRepo userRepo ;

    public UserServiceImpl(UserRepo userRepo) {
        this.userRepo = userRepo;
    }

    @Override
    public User getUserById(long id){
        return userRepo.findById(id);
    }

    @Override
    public Optional<User> getUserByName(String userName) {
        return userRepo.findByUsername(userName);
    }

    @Override
    public Map<String,Object> searchLogByUserName(String username){
        Optional<User> user = userRepo.findByUsername(username);
        Map<String,Object> response = new HashMap<>();

        if(user.isPresent()){//找到对应的用户
            response.put("exists",true);
            response.put("message","查找成功");
            response.put("user",user.get());
        }else {
            response.put("exists",false);
            response.put("message","查询结果为空");
        }

        return response;
    }

    @Override
    public Map<String, Object> banByUserName(String username) {
        userRepo.updateUserStatusByUsername(username,0);//表示是否被禁用，0表示被禁用，1表示没被禁用

        Map<String,Object> response = new HashMap<>();
        response.put("success",true);
        return response;
    }

    @Override
    public Map<String, Object> activeByUserName(String username) {
        userRepo.updateUserStatusByUsername(username,1);//表示是否被禁用，0表示被禁用，1表示没被禁用

        Map<String,Object> response = new HashMap<>();
        response.put("success",true);
        return response;
    }

    @Override
    public User getUserByEmail(String Email) {
        return userRepo.findByEmail(Email);
    }

    @Override
    public User getUserByPhone(String ph) {
        return userRepo.findByPhoneNumber(ph);
    }

    @Override
    public List<User> getUserByRole(String role) {
        return userRepo.findByRole(role);
    }

    @Override
    public List<User> getUserByRegistrationDate(Date registrationDate) {
        return userRepo.findByRegistrationDate(registrationDate);
    }

    @Override
    public User createUser(User user) {
        user.setRegistrationDate(new Date());
        return userRepo.save(user);
    }

    @Override
    public Optional<User> getUserById(Long id) {
        return userRepo.findById(id);
    }

    @Override
    public List<User> getAllUsers() {
        return userRepo.findAll();
    }

    public Map<String,Object> getAllUsersList(){
        List<User> userList = userRepo.findAll();
        Map<String,Object> response = new HashMap<>();

        if(userList.size() == 0){
            response.put("exists",false);
            response.put("message","查找结果为空");
        }else{
            response.put("exists",true);
            response.put("message","查找成功");
            response.put("userList",userList);
        }
        return  response;
    }

    @Override
    public User updateUser(Long id, User user) {
        Optional<User> existingUserOptional = userRepo.findById(id);
        if (existingUserOptional.isPresent()) {
            User existingUser = existingUserOptional.get();
            existingUser.setName(user.getName());
            existingUser.setUsername(user.getUsername());
            existingUser.setPassword(user.getPassword());
            existingUser.setEmail(user.getEmail());
            existingUser.setPhoneNumber(user.getPhoneNumber());
            existingUser.setRole(user.getRole());
            return userRepo.save(existingUser);
        } else {
            return null; // Or throw an exception
        }
    }

    @Override
    public boolean deleteUser(Long id) {
        if (userRepo.existsById(id)) {
            userRepo.deleteById(id);
            return true;
        } else {
            return false; // Or throw an exception
        }
    }

    @Override
    public Optional<User> findByUsername(String username) {
        return userRepo.findByUsername(username);
    }

}
